package com.example.zheng.els2.activity;


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.example.zheng.els2.R;

import java.util.HashSet;
import java.util.Set;

public class Register extends Activity implements View.OnClickListener{

    private EditText name;
    private EditText pass;
    private EditText repass;
    private Button register;
    private Button back;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //去掉手机上的状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.register);

        name=(EditText)findViewById(R.id.name);
        pass=(EditText)findViewById(R.id.pass);
        repass=(EditText)findViewById(R.id.repass);
        register=(Button)findViewById(R.id.register2);
        back=(Button)findViewById(R.id.back);

        register.setOnClickListener(this);
        back.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.register2:
                String usename=name.getText().toString();
                String passW=pass.getText().toString();
                String passW1=repass.getText().toString();
                SharedPreferences sharedPreferences=getSharedPreferences("data1",MODE_PRIVATE);
                Set<String> set=sharedPreferences.getStringSet(usename,null);
                if(set!=null){
                    Toast.makeText(this,"该用户已存在",Toast.LENGTH_SHORT).show();
                }else{
                    if(passW.equals(passW1)&&!"".equals(passW)&&!"".equals(usename)){
                        set=new HashSet<>();
                        set.add(passW);
                        sharedPreferences.edit().putStringSet(usename,set).apply();
                        Toast.makeText(this,"注册成功，请登录",Toast.LENGTH_SHORT).show();
                        finish();

                    }else{
                        Toast.makeText(this,"密码不匹配",Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }

    }
}
